package com.ucar.training.listener;

import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.http.HttpServletRequest;

/**
 * @author XiongMM
 * @date 2019/7/26 15:45
 * @description
 * 这个监听中提供了对HttpRequest的参数在进行新增、修改、删除时的监听，如果在应用层代码中修改请求的参数：

request.setAttribute("testContextAttribute", "abc");

request.setAttribute("testContextAttribute", "def");

request.removeAttribute("testContextAttribute");
那么监听器可能会有这样的输出：

http://localhost:9080/dangwebx-tst/tms/bms/billingQaReportController/list.do新增request参数，name:testContextAttribute,value:abc

http://localhost:9080/dangwebx-tst/tms/bms/billingQaReportController/list.do修改request参数，name:testContextAttribute,value:abc

http://localhost:9080/dangwebx-tst/tms/bms/billingQaReportController/list.do删除request参数，name:testContextAttribute,value:def

在实际项目中，HttpRequestAttribute的监听器输出的内容可能比想象中的要多，因为不管是java还是项目中的其他组件都有可能对请求进行一些必要的装配，所以可能会出现一些这样的输出：

http://localhost:9080/dangwebx-tst/tms/bms/billingQaReportController/list.do新增request参数，name:javax.servlet.jsp.jstl.fmt.locale.request,value:zh_CN

http://localhost:9080/dangwebx-tst/tms/bms/billingQaReportController/list.do新增request参数，name:javax.servlet.jsp.jstl.fmt.localizationContext.request,value:org.springframework.web.servlet.support.JstlUtils$SpringLocalizationContext@486f7c41

http://localhost:9080/dangwebx-tst/tms/bms/billingQaReportController/list.do删除request参数，name:__spring_security_scpf_applied,value:true

http://localhost:9080/dangwebx-tst/tms/bms/billingQaReportController/list.do删除request参数，name:__dangwebx_context_persistence_filter,value:true

http://localhost:9080/dangwebx-tst/tms/bms/billingQaReportController/list.do删除request参数，name:encodingFilter.FILTERED,value:true

http://localhost:9080/dangwebx-tst/tms/bms/billingQaReportController/list.do删除request参数，name:openSessionInViewFilter.FILTERED,value:true
--------------------
 */
public class MyServletRequestAttributeListener implements ServletRequestAttributeListener {
    @Override
    public void attributeAdded(ServletRequestAttributeEvent arg0) {
        HttpServletRequest servletRequest=(HttpServletRequest)arg0.getServletRequest();
        System.out.println(servletRequest.getRequestURL().toString()+"新增request参数，name:"+arg0.getName()+",value:"+arg0.getValue());

    }

    @Override
    public void attributeRemoved(ServletRequestAttributeEvent arg0) {
        HttpServletRequest servletRequest=(HttpServletRequest)arg0.getServletRequest();
        System.out.println(servletRequest.getRequestURL().toString()+"删除request参数，name:"+arg0.getName()+",value:"+arg0.getValue());

    }

    @Override
    public void attributeReplaced(ServletRequestAttributeEvent arg0) {
        HttpServletRequest servletRequest=(HttpServletRequest)arg0.getServletRequest();
        System.out.println(servletRequest.getRequestURL().toString()+"修改request参数，name:"+arg0.getName()+",value:"+arg0.getValue());

    }
}
